﻿# Determines who can command troops; filters who shows up in the list
# Root is the potential commander
# scope:army_owner is who owns the army to command
can_command_troops = {
	can_be_commander_basic_trigger = { ARMY_OWNER = scope:army_owner }
}

# Determines who can command troops; will still show up in the list, with a breakdown explaining why they can't command
# Root is the potential commander
# scope:army_owner is who owns the army to command
can_command_troops_now = {
	can_be_commander_now_trigger = { ARMY_OWNER = scope:army_owner }
}

# Determines if a barony should be handed to a GHW beneficiary, or instead have a baron generated. If true, goes to a beneficiary
# Root is barony province
# scope:faith is the faith controlling the GHW
ghw_give_barony_to_beneficiary = {
	has_holding_type = castle_holding
}

# Determines if a faith can be created, on top of the piety requirement, and the doctrine requirements
# Also handles reforming a faith, check for the unreformed doctrine to enable those additional checks
# Root is the faith creator
faith_creation = {
	trigger_if = {
		limit = {
			highest_held_title_tier = tier_county
			is_independent_ruler = no
		}
		custom_description = {
			text = "faith_creation_duchy_or_higher"
			highest_held_title_tier >= tier_duchy
		}
	}

	is_adult = yes
	is_at_war = no
	custom_description = {
		text = "character_is_not_real_head"
		NOT = { faith.religious_head = root }
	}

	custom_description = {
		text = "character_can_only_create_one_faith"
		NOT = { exists = var:has_created_a_faith }
	}



	trigger_if = {
		limit = { 
			has_game_rule = no_heresy_creation
			NOT = { faith = { has_doctrine_parameter = unreformed } }
		}
		custom_description = {
			text = "no_heresy_creation"
			always = no
		}
	}
	
	trigger_if = {
		limit = { 
			has_game_rule = no_pagan_reformation 
			faith = { has_doctrine_parameter = unreformed }
		}
		custom_description = {
			text = "no_pagan_reformation"
			always = no
		}
	}
	
	trigger_if = {
		limit = {
			NOT = { has_game_rule = no_pagan_reformation }
			faith = { has_doctrine_parameter = unreformed }
		}
		
		NOT = {
			custom_description = {
				text = faith_has_been_reformed
				object = faith
				exists = faith.var:has_been_reformed
			}
		}

		trigger_if = {
			limit = { has_game_rule = pagan_reformation_hs_1 }
			faith = {
				num_realm_holy_sites_faithful_holders = {
					CHARACTER = root
					COUNT = 1
				}
			}
		}
		trigger_else_if = {
			limit = { has_game_rule = pagan_reformation_hs_2 }
			faith = {
				num_realm_holy_sites_faithful_holders = {
					CHARACTER = root
					COUNT = 2
				}
			}
		}
		trigger_else_if = {
			limit = { has_game_rule = pagan_reformation_hs_3 }
			faith = {
				num_realm_holy_sites_faithful_holders = {
					CHARACTER = root
					COUNT = 3
				}
			}
		}
		trigger_else_if = {
			limit = { has_game_rule = pagan_reformation_hs_4 }
			faith = {
				num_realm_holy_sites_faithful_holders = {
					CHARACTER = root
					COUNT = 4
				}
			}
		}
		trigger_else_if = {
			limit = { has_game_rule = pagan_reformation_hs_5 }
			faith = {
				num_realm_holy_sites_faithful_holders = {
					CHARACTER = root
					COUNT = 5
				}
			}
		}
		trigger_else = { always = yes }
	}
}

# Determines if a character can convert faith via the convert faith UI, on top of the piety requirement
# Root is the faith creator
# scope:new_faith is the faith they want to convert to
faith_conversion = {
	is_adult = yes
	NOT = { faith.religious_head = root }
	is_in_ongoing_great_holy_war = no
}

# Determines if the mother of a character should spread the house to a bastard.
# Requirements: One of both parents should exists. One of both parents should have a house.
# Root is the bastard
should_mother_give_house_to_bastard = {
	should_mother_give_house_to_bastard_trigger = yes
}

# Determines if a character may be appointed as a knight. Checked during automatic knight assignment
# Root is the potential knight
can_be_knight = {
	trigger_if = {
		limit = { 
			OR = {
				is_landed = no
				root = root.court_owner 
			}
			NOT = { exists = liege } 
		}
		can_be_knight_trigger = { ARMY_OWNER = root.court_owner }
	} 
	trigger_else = {
		can_be_knight_trigger = { ARMY_OWNER = liege }
	}
}

# Determines if a character is allowed to be granted titles by another character
# Root is the target character
# scope:liege is the granter
allowed_to_be_granted_titles_by = {
	can_be_granted_titles_by = { RULER = scope:liege }
}

is_character_allowed_to_be_player = {
	OR = {
		has_game_rule = allow_unplayables
		AND = {
			custom_description = {
				text = "GAME_OVER_CANNOT_PLAY_THEOCRACY"
				NOT = { has_government = theocracy_government }
			}
			custom_description = {
				text = "GAME_OVER_CANNOT_PLAY_REPUBLIC"
				NOT = { has_government = republic_government }
			}
		}
	}
}

# Determines if a secret is considered available for blackmail for use in the GUI (icon/tooltip listing a secret you know as usable for blackmail)
# Scopes the same as a secret_type's is_valid, plus:
# root -> the secret
# scope:target -> the target character
# scope:blackmailer -> the blackmailing character
# The GUI only checks secrets the character is a participant in, that you know of, that have not been spent by you, and that are not revealed
is_secret_available_for_blackmail = {
	is_blackmailable_secret_trigger = {
	 	PARTICIPANT = scope:target
	 	BLACKMAILER = scope:blackmailer
	}
}

# Is this character allowed to join factions in general. See also passes_faction_soft_block.
# Root: Character being evaluated
# scope:target: the target character of the faction
passes_faction_hard_block = {
	custom_description = {
		text = "FACTION_CANT_JOIN_IF_TARGET"
		NOT = { root = scope:target }
	}

	custom_description = {
		text = "FACTION_CANT_JOIN_CREATE_ADULT"
		is_adult = yes
	}
	
	custom_description = {
		text = "FACTION_CANT_JOIN_CREATE_ALLIED"
		trigger_if = {
			limit = { exists = scope:target }
			NOT = { is_allied_to = scope:target }
		}
	}

	custom_description = {
		text = "FACTION_CANT_JOIN_CREATE_HOOK"
		trigger_if = {
			limit = { exists = scope:target }
			scope:target = {
				NOT = { has_strong_hook = root }
			}		
		}
	}

	custom_description = {
		text = "FACTION_CANT_JOIN_CREATE_IMPRISONED"
		is_imprisoned = no		
	}	

	custom_description = {
		text = "FACTION_CANT_JOIN_CREATE_INCAPABLE"
		NOT = { has_trait = incapable }
	}

	custom_description = {
		text = "character_blocked_from_joining"
		NOT = { has_character_flag = joining_faction_block }
	}

	custom_description = {
		text = "faction_relation_blocks_joining_friend"
		trigger_if = {
			limit = { exists = scope:target }
			NOT = {
				root = { has_relation_friend = scope:target }
			}
		}
	}

	custom_description = {
		text = "faction_relation_blocks_joining_lover"
		trigger_if = {
			limit = { exists = scope:target }
			NOR = {
				root = { has_relation_lover = scope:target }
				root = { has_secret_relation_lover = scope:target }
			}
		}
	}
}

# Is this character allowed to join factions. It is a soft block because factions can set a toggle to ignore this block on the faction type.
# Root: Character being evaluated
passes_faction_soft_block = {
	custom_description = {
		text = "FACTION_SOFT_BLOCK_IS_NOT_COURT_CHAPLAIN"
		NOT = { has_council_position = councillor_court_chaplain }
	}
}

# Is this faction dangerous
# Root = Faction being evaluated
is_dangerous_faction = {
	is_dangerous_faction_trigger = yes
}

# Is the alliance reason still valid
# Root = First character of alliance reason
# scope:second = Second character of alliance reason
is_alliance_valid = {
	OR = {
		any_spouse = {
			this = scope:second
		}
		betrothed = scope:second
		is_parent_of = scope:second
		is_child_of = scope:second
		is_sibling_of = scope:second
		scope:second = {
			has_opinion_modifier = {
				modifier = perk_negotiated_alliance_opinion
				target = root
			}
		}
	}
}

# Can the character designate an heir? Used for the Realm screen
can_designate_heir = {
	can_designate_heir_trigger = yes
}


# Does the character care about having a council position? Used for powerful vassals that have no council position for whether they should get the opinion penalty
cares_about_powerful_vassal_council_position = {
	can_be_councillor_basics_trigger = yes
}

# Does the character approve of succession law changes? Used for the succession law change GUI to designate oppose vs. approve for powerful vassals
approves_of_succession_law_change = {
	opposes_succession_law_change_trigger = no
}

# Does the character get a second chance when rolling a natural death
has_natural_death_second_chance = {
	OR = {
		AND = {
			has_perk = know_thyself_perk
			NOT = { has_character_flag = know_thyself_has_triggered_delay }
		}
		has_character_flag = know_thyself_perk_delay_period
	}
}

# Can the character refund their perks? Scope is the character
can_refund_perks = {
	perk_points_assigned >= 1
	custom_description = {
		text = already_reset_perks
		NOT = {
			has_character_flag = has_refunded_perks
		}
	}
}

# Can the character join the defender in a holy war? Only characters' of the defender's faith are considered. Player characters are not considered.
# Scope is the potential joiner
# scope:attacker is the attacker
# scope:defender is the defender
can_defensively_join_holy_war = {

}

# Can a character be fire from being a councillor
# root = council owner
# scope:councillor = councillor attempting to be fired
can_fire_councillor = {
	custom_description = {
		text = blocked_from_remove_councillor
  		subject = root
  		object = scope:councillor
		OR = {
			NOT = { exists = scope:councillor }
			NOT = {
				scope:councillor = {
					is_blocked_from_being_fired_from_council_trigger = { COURT_OWNER = root }
				}
			}	
		}
	}
}

# Can this character go on raids?
# root: the potential raider
can_raid = {
	#Scripted trigger used so that we can call on the same conditions in script.
	can_raid_trigger = yes
}

# Can this character start a new raid right now? (can_raid will always get checked first)
# root: the potential raider
can_start_raid = {
	is_at_war = no
}

# Can this character cross water (sea, rivers) while on raid?
# root: the potential raider
can_raid_across_water = {
	culture = {
		OR = {
			has_innovation = innovation_longboats
			has_innovation = innovation_african_canoes
		}
	}
}

# Can this character traverse rivers using boats?
# root: the owner of the army
can_traverse_river = {
	culture = {
		OR = {
			has_innovation = innovation_longboats
			has_innovation = innovation_african_canoes
		}
	}
}

#Is this character hard blocked from backing a scheme?
#Scope it to potential backer
is_hard_blocked_from_schemes = {
	is_incapable = yes
}

# Will AI for this character try to arrange matrilineal marriages?
# Note that the AI can still arrange patrilineal marriages based on the SPOUSE_SCORE_LINEALITY_MISMATCH_SCORE AI define
# root: the arranging AI character
# scope:secondary_actor: the character to be married
ai_wants_matrilineal_marriage = {
	#MGR#
	NOT = { has_game_rule = never_matri }
	OR = {
		faith = { has_doctrine_parameter = female_dominated_law }
		NOT = { has_game_rule = more_restricted_matri }
	}
	#END MGR#
	scope:secondary_actor = {
		is_female = yes
	}
	trigger_if = { # AI's always want to matrilineally marry their Dynasty members
		limit = {
			has_game_rule = matrilineal_marriages_always
		}
		scope:secondary_actor.dynasty = root.dynasty
	}
	trigger_else_if = { # AI's want matrilineal marriages if female dominated or equal, but not otherwise
		limit = {
			has_game_rule = matrilineal_marriages_female_and_equal
		}
		OR = { # Either Matrilineal Marriages are the norm
			has_realm_law = female_only_law
			has_realm_law = female_preference_law
			faith = {
				has_doctrine_parameter = female_dominated_law
			}
			AND = {
				exists = dynasty
				scope:secondary_actor.dynasty = root.dynasty
				root = {
					has_realm_law = equal_law
				}
				OR = {
					scope:secondary_actor = root # Either I am a woman
					OR = {
						root.primary_title = { # Or it's my first three heirs
							place_in_line_of_succession = {
								target = scope:secondary_actor
								value <= 3
							}
						}
						scope:secondary_actor = {
							any_parent = { # Or my first heir's oldest child
								root.primary_title = {
									place_in_line_of_succession = {
										target = scope:secondary_actor
										value <= 1
									}
								}
							}
							NOT = {
								any_sibling = {
									age > scope:secondary_actor.age
								}
							}
						}
					}
				}
			}
		}
	}
	trigger_else_if = { # Only Female Dominated realms want matrilineal marriages
		limit = {
			has_game_rule = matrilineal_marriages_never
		}
		OR = { # Matrilineal Marriages are the norm
			has_realm_law = female_only_law
			has_realm_law = female_preference_law
			faith = {
				has_doctrine_parameter = female_dominated_law
			}
		}
	}
	trigger_else_if = { # Default, but no player exception
		limit = {
			has_game_rule = matrilineal_marriages_no_player_exception
		}
		OR = { # Either Matrilineal Marriages are the norm
			has_realm_law = female_only_law
			has_realm_law = female_preference_law
			faith = {
				has_doctrine_parameter = female_dominated_law
			}
			AND = { # Or there's a good reason to pursue them, such as being of a Reputable dynasty or ruling a Duchy
				exists = dynasty
				scope:secondary_actor.dynasty = root.dynasty
				OR = {
					scope:secondary_actor = root # Either I am a woman
					root.primary_title = { # It's my primary heir
						place_in_line_of_succession = {
							target = scope:secondary_actor
							value <= 1
						}
					}
					AND = { # Or I'm under Equal, and it's my first three heirs
						has_realm_law = equal_law
						OR = {
							root.primary_title = {
								place_in_line_of_succession = {
									target = scope:secondary_actor
									value <= 3
								}
							}
							scope:secondary_actor = {
								any_parent = { # Or my first heir's oldest child
									root.primary_title = {
										place_in_line_of_succession = {
											target = scope:secondary_actor
											value <= 1
										}
									}
								}
								NOT = {
									any_sibling = {
										age > scope:secondary_actor.age
									}
								}
							}
						}
					}
				}
				root = {
					OR = {
						highest_held_title_tier >= tier_duchy
						dynasty = { dynasty_prestige_level >= 4 }
						has_realm_law = equal_law
					}
				}
			}
		}
	}
	trigger_else = { # Default rules	
		OR = { # Either Matrilineal Marriages are the norm
			has_realm_law = female_only_law
			has_realm_law = female_preference_law
			faith = {
				has_doctrine_parameter = female_dominated_law
			}
			AND = { # Or there's a good reason to pursue them, such as being of a Reputable dynasty or ruling a Duchy
				exists = dynasty
				scope:secondary_actor.dynasty = root.dynasty
				OR = {
					scope:secondary_actor = root # Either I am a woman
					root.primary_title = { # It's my primary heir
						place_in_line_of_succession = {
							target = scope:secondary_actor
							value <= 1
						}
					}
					AND = { # Or I'm under Equal, and it's my first three heirs
						has_realm_law = equal_law
						OR = {
							root.primary_title = {
								place_in_line_of_succession = {
									target = scope:secondary_actor
									value <= 3
								}
							}
							scope:secondary_actor = {
								any_parent = { # Or my first heir's oldest child
									root.primary_title = {
										place_in_line_of_succession = {
											target = scope:secondary_actor
											value <= 1
										}
									}
								}
								NOT = {
									any_sibling = {
										age > scope:secondary_actor.age
									}
								}
							}
						}
					}
				}
				root = {
					OR = {
						highest_held_title_tier >= tier_duchy
						dynasty = { dynasty_prestige_level >= 4 }
						has_realm_law = equal_law
						any_player = { # Very close to a player
							exists = dynasty
							dynasty = scope:secondary_actor.dynasty
							is_close_family_of = scope:secondary_actor
						}
					}
				}
			}
		}
	}
}

# Is this character blocked from having buildings enabled?
#Scope it to potential backer
buildings_enabled = {
	custom_description = {
		text = vastly_over_domain_limit_trigger
		trigger_if = {
			limit = {
				is_ai = no
			}
			domain_size_excluding_grace_period < building_disable_inefficient_value
		}
	}
}

# Can this character potentially call its ally to war (note, war doesn't necessarily exist yet)
# Used by the AI to account for allies when determining wars to declare
# Also used in the war declaration screen to filter out allies that definitely cannot be called
# root: the potential caller
# scope:ally: the ally
can_potentially_call_ally = {
	can_potentially_call_ally_trigger = {
		WARRIOR = root
		JOINER = scope:ally
	}
}
